package com.auto.survey.activity.adjustment.adjustment_high

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.auto.survey.R
import com.auto.survey.activity.adjustment.adjustment_high.entity.Project
import com.auto.survey.util.Helper

class HighAdjustMentAdapter : Adapter<HighAdjustMentAdapter.MyViewHodler>() {

    private var listPorjs:MutableList<Project>?=null
    private var mItemClickListener:ItemClickListener?= null

    fun setItems(data: MutableList<Project>) {
        listPorjs = data
        notifyDataSetChanged()
    }

    fun setOnItemClickListener(itemclick: ItemClickListener) {
        mItemClickListener = itemclick
    }

    override fun getItemCount(): Int {
       if(listPorjs!= null && listPorjs?.size!! > 0){
           return listPorjs?.size!!
       }
        return 0
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHodler {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.adapter_adjustment_high, parent, false)
        return MyViewHodler(view)
    }



    override fun onBindViewHolder(holder: MyViewHodler, position: Int) {

        holder.projectName.text = listPorjs?.get(position)?.name ?: ""
        holder.projectNum.text = (listPorjs?.get(position)?.pointCount ?: "").toString()
        holder.projectDate.text = listPorjs?.get(position)?.createTime?.let {
            Helper.formatTimestamp(it)
        }

        holder.cardview.setOnClickListener {
            listPorjs?.get(position)?.let { it1 -> mItemClickListener?.onClickItem(it1, position) }
        }
    }

    inner class MyViewHodler(view: View) : ViewHolder(view) {
        val cardview = view.findViewById<CardView>(R.id.proj_cardview)
        val projectName = view.findViewById<TextView>(R.id.tv_proj_name)
        val projectNum = view.findViewById<TextView>(R.id.tv_proj_num)
        val projectDate = view.findViewById<TextView>(R.id.tv_proj_date)
    }
}